package com.fdb.efp.loan.service.impl;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.fdb.efp.loan.service.domain.BdIntFeeDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.fdb.efp.loan.service.dao.BdIntFeeDetailDao;
import com.fdb.efp.loan.service.facade.BdIntFeeDetailService;
import com.fdb.efp.loan.service.facade.BdIntFeeDetailTempService;
import com.fdb.efp.loan.service.vo.BdIntFeeDetailTempVO;
import com.fdb.efp.loan.service.vo.BdIntFeeDetailVO;

/**
 * <p>
 * ServiceImpl: 手工减免息费明细分期表
 * <p>
 * 
 * @author ligm
 * @date 2019-03-04
 */
@Service("bdIntFeeDetailService")
public class BdIntFeeDetailServiceImpl implements BdIntFeeDetailService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(BdIntFeeDetailServiceImpl.class);

	@Autowired
	private BdIntFeeDetailDao bdIntFeeDetailDao;

	@Autowired
	@Qualifier("bdIntFeeDetailTempService")
	private BdIntFeeDetailTempService bdIntFeeDetailTempService;

	/**
	 * 新增操作
	 */
	public int insertBdIntFeeDetail(BdIntFeeDetailVO inBdIntFeeDetailVo) {
		logger.debug("当前新增数据为:" + inBdIntFeeDetailVo.toString());
		int num = 0;
		try {
			BdIntFeeDetail bdIntFeeDetail = new BdIntFeeDetail();
			beanCopy(inBdIntFeeDetailVo, bdIntFeeDetail);
			num = bdIntFeeDetailDao.insertBdIntFeeDetail(bdIntFeeDetail);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BdIntFeeDetailVO inBdIntFeeDetailVo) {
		logger.debug("当前删除数据条件为:" + inBdIntFeeDetailVo);
		int num = 0;
		try {
			BdIntFeeDetail bdIntFeeDetail = new BdIntFeeDetail();
			beanCopy(inBdIntFeeDetailVo, bdIntFeeDetail);
			num = bdIntFeeDetailDao.deleteByPk(bdIntFeeDetail);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBdIntFeeDetailVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	public int updateByPk(BdIntFeeDetailVO inBdIntFeeDetailVo) {
		logger.debug("当前修改数据为:" + inBdIntFeeDetailVo.toString());
		int num = 0;
		try {
			BdIntFeeDetail bdIntFeeDetail = new BdIntFeeDetail();
			beanCopy(inBdIntFeeDetailVo, bdIntFeeDetail);
			num = bdIntFeeDetailDao.updateByPk(bdIntFeeDetail);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inBdIntFeeDetailVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	public BdIntFeeDetailVO queryByPk(BdIntFeeDetailVO inBdIntFeeDetailVo) {

		logger.debug("当前查询参数信息为:" + inBdIntFeeDetailVo);
		try {
			BdIntFeeDetail queryBdIntFeeDetail = new BdIntFeeDetail();
			beanCopy(inBdIntFeeDetailVo, queryBdIntFeeDetail);
			BdIntFeeDetail queryRslBdIntFeeDetail = bdIntFeeDetailDao.queryByPk(queryBdIntFeeDetail);
			if (Objects.nonNull(queryRslBdIntFeeDetail)) {
				BdIntFeeDetailVO outBdIntFeeDetailVo = beanCopy(queryRslBdIntFeeDetail, new BdIntFeeDetailVO());
				logger.debug("当前查询结果为:" + outBdIntFeeDetailVo.toString());
				return outBdIntFeeDetailVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdIntFeeDetailVO> queryAllOwner(BdIntFeeDetailVO bdIntFeeDetailVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BdIntFeeDetailVO> list = null;
		try {
			List<BdIntFeeDetail> bdIntFeeDetails = bdIntFeeDetailDao.queryAllOwnerByPage(bdIntFeeDetailVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + bdIntFeeDetails.size());
			pageSet(bdIntFeeDetails, bdIntFeeDetailVo);
			list = (List<BdIntFeeDetailVO>) beansCopy(bdIntFeeDetails, BdIntFeeDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdIntFeeDetailVO> queryAllCurrOrg(BdIntFeeDetailVO bdIntFeeDetailVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BdIntFeeDetail> bdIntFeeDetails = bdIntFeeDetailDao.queryAllCurrOrgByPage(bdIntFeeDetailVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + bdIntFeeDetails.size());
		List<BdIntFeeDetailVO> list = null;
		try {
			pageSet(bdIntFeeDetails, bdIntFeeDetailVo);
			list = (List<BdIntFeeDetailVO>) beansCopy(bdIntFeeDetails, BdIntFeeDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BdIntFeeDetailVO> queryAllCurrDownOrg(BdIntFeeDetailVO bdIntFeeDetailVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BdIntFeeDetail> bdIntFeeDetails = bdIntFeeDetailDao.queryAllCurrDownOrgByPage(bdIntFeeDetailVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + bdIntFeeDetails.size());
		List<BdIntFeeDetailVO> list = null;
		try {
			pageSet(bdIntFeeDetails, bdIntFeeDetailVo);
			list = (List<BdIntFeeDetailVO>) beansCopy(bdIntFeeDetails, BdIntFeeDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 
	 * 批量新增手工减免息信息 <br>
	 * 0.1:ludayong:2019年3月8日 下午2:37:18:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月8日 下午2:37:18
	 * @date 2019年3月8日 下午2:37:18
	 * @version 0.1
	 */
	@Override
	public int batchInsertBdIntFeeDetail(List<BdIntFeeDetailTempVO> addList) {
		logger.debug("当前新增数据条数为:" + addList.size());
		List<BdIntFeeDetail> bdIntFeeDetailList = new ArrayList<BdIntFeeDetail>();
		int num = 0;
		try {
			num = bdIntFeeDetailDao.batchInsertBdIntFeeDetail(addList);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 
	 * 批量更新手工减免息信息 <br>
	 * 0.1:ludayong:2019年3月8日 下午2:37:18:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月8日 下午2:37:18
	 * @date 2019年3月8日 下午2:37:18
	 * @version 0.1
	 */
	@Override
	public int batchUpdateBdIntFeeDetail(List<BdIntFeeDetailTempVO> updateList) {
		logger.debug("当前修改数据为:" + updateList.size() + "条");
		List<BdIntFeeDetail> bdIntFeeDetailList = new ArrayList<BdIntFeeDetail>();
		int num = 0;
		try {
			num = bdIntFeeDetailDao.batchUpdateBdIntFeeDetail(updateList);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("修改的数据条数为" + num);
		return num;
	}

	/**
	 * 
	 * 同步托管机构资金划扣临时表数据到托管机构资金划扣正式表有数据则更新，无则新增 <br>
	 * 0.1:ludayong:2019年3月23日 下午12:09:28:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年3月23日 下午12:09:28
	 * @date 2019年3月23日 下午12:09:28
	 * @version 0.1
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int insertOrUpdateBdIntFeeDetail(List<BdIntFeeDetailTempVO> list) {
		LocalDate today = LocalDate.now();
		String msg1 = "【手工减免息费明细表】数据同步开始..." + today;
		logger.info(msg1, "message{}");
		List<BdIntFeeDetail> bdIntFeeDetailList = new ArrayList<BdIntFeeDetail>();
		int num = 0;
		try {
			bdIntFeeDetailList = (List<BdIntFeeDetail>) beansCopy(list, BdIntFeeDetail.class);
			num = bdIntFeeDetailDao.insertOrUpdateBdIntFeeDetail(bdIntFeeDetailList);
			String num_msg = "【手工减免息费明细表】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
		} catch (Exception e) {
			String msg2 = "【手工减免息费明细表】插入或更新数据出现异常:" + e;
			logger.error(msg2, "message{}");
			num = -1;
		}
		return num;
	}
}
